home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-08-06 | 14.5 KB | 534 lines | [TEXT/CWIE] |
- /// DemoAccessor.cp
- // Copyright: © 1994 - 1997 by Apple Computer, Inc., all rights reserved.
-
-
- #include "DemoAccessor.h"
-
- #ifdef DEBUG_NEW
- #include <DebugNew.h>
- #endif
-
- #include <stdio.h>
- #include <string.h>
-
- #if __profile__
- #include <profiler.h>
- #endif
-
- void PrintDocName(IADoc* doc);
- void PrintDocName(IADoc* doc) {
- uint32 docNameLength;
- char* docName = (char*)doc->GetName(&docNameLength);
- // IAAssert(strlen(docName) == docNameLength);
- IAAssertion(strlen(docName) == docNameLength, "mismatch document names", IAAssertionFailure);
- printf("%s", docName);
- IAFreeArray(docName);
- }
-
- void DemoAccessor::Initialize() {
- time_t initStartTime = time(NULL);
- DEMOACCESSORCLASS::Initialize();
- time_t initTime = time(NULL) - initStartTime;
- struct tm* timeStruct = localtime(&initTime);
- char buffer[80];
- strftime(buffer, 80, "accessor initialization: %H hours, %M minutes and %S seconds.\n", timeStruct);
- printf(buffer);
- }
-
- bool DemoAccessor::IsHit(IAIndex* index, const IADoc* doc) {
- #pragma unused(index)
- bool result = true;
- if (firstChar) {
- uint32 docNameLength;
- char* docName = (char*)doc->GetName(&docNameLength);
- if (!docNameLength) {
- result = false;
- } else if (docName[0] != firstChar) {
- result = false;
- }
- IAFreeArray(docName);
- }
- return result;
- }
-
- //// Calling the IAAccessor
-
- bool DemoRankedProgress(const RankedProgress* progress, void* data);
- bool DemoRankedProgress(const RankedProgress* progress, void* data) {
- #pragma unused (data)
- printf("searching: %4.1f ", progress->GetPercent());
- if (progress->GetTerm()) {
- printf("%s", (char*)progress->GetTerm()->GetData());
- }
- if (progress->GetDocument()) {
- PrintDocName(progress->GetDocument());
- }
- printf("\n");
- return false;
- }
-
- const MaxResultCount = 15;
- const uint32 kMaxDocuments = 10;
-
- void DemoRankedSearchInternal (DemoAccessor* accessor, char* query, IADoc** feedback, uint32 nrq);
- void DemoRankedSearchInternal (DemoAccessor* accessor, char* query, IADoc** feedback, uint32 nrq) {
- RankedQueryDoc rqd1[kMaxDocuments];
- if (nrq > kMaxDocuments) nrq = kMaxDocuments;
- printf("Query: %s\n", query); // display query
- for (int i = 0; i < nrq; i ++) {
- printf("Feedback: ");
- PrintDocName(feedback[i]);
- printf("\n");
- }
-
- time_t start = time(NULL);
-
-
- RankedHit* results[MaxResultCount]; // allocate array for results
- // RankedQueryDoc rqd(feedback, TermIndex::IANarrow(accessor->indices[0]));
-
- for (int i = 0; i <nrq; i++) {
- rqd1[i].doc = feedback[i];
- rqd1[i].index = TermIndex::IANarrow(accessor->GetIndices()[0]);
- }
- // execute query
- uint32 resultCount = accessor->RankedSearch((byte*)query, strlen(query), // query string
- rqd1, nrq, // feedback doc
- results, MaxResultCount, 4, // result array
- &DemoRankedProgress, 30, NULL); // progress args
-
- time_t duration = time(NULL) - start;
- struct tm* timeStruct = localtime(&duration);
- char buffer[80];
- strftime(buffer, 80, "search time: %H hours, %M minutes and %S seconds.\n", timeStruct);
- printf(buffer);
-
- printf("%lu hits\n", resultCount); // display results
- for (uint32 i = 0; i < resultCount; i++) {
- RankedHit* hit = results[i];
- printf("%5.2f : ", hit->GetScore());
- PrintDocName(hit->GetDocument());
- printf(" [");
- for (uint32 j = 0; j < hit->GetMatchingTermsLen(); j++)
- printf(" %s", hit->GetMatchingTerms()[j]->GetData());
- printf("]\n");
- delete hit;
- }
- }
-
- void DemoRankedSearch (char* query, IADoc* feedback = NULL, char firstChar = NULL);
- void DemoRankedSearch (char* query, IADoc* feedback, char firstChar) {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(false);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
- /*
- // make a second storage
- IAStorage* storage2 = DEMOSTORAGE("\p2.index");
- IADeleteOnUnwind delStorage2(storage2);
- storage2->Open(false);
- // make a second index in this storage
- DemoIndex index2(storage2);
- index2.Open();
- */
- const uint32 nIndices = 1;
- DEMOACCESSORINDEXCLASS* indices[nIndices]; // make indices
- indices[0] = &index;
- //indices[1] = &index2;
-
- DemoAccessor accessor(indices, nIndices); // make appropriate accessor
- accessor.firstChar = firstChar;
- accessor.Initialize();
-
- DemoRankedSearchInternal(&accessor, query, &feedback, 0);
- }
-
- void DemoBooleanRankedSearchInternal (DemoAccessor* accessor, char* query);
- void DemoBooleanRankedSearchInternal (DemoAccessor* accessor, char* query) {
- RankedQueryDoc rqd1[kMaxDocuments];
-
- printf("Query: %s\n", query); // display query
-
- time_t start = time(NULL);
-
-
- RankedHit* results[MaxResultCount]; // allocate array for results
- uint32 resultCount = 0;
- // RankedQueryDoc rqd(feedback, TermIndex::IANarrow(accessor->indices[0]));
- #ifdef DEMOINDEXISINVERTED
-
- // execute query
- ((InvertedAccessor*)accessor)->SetBooleanAndOperator('+');
- ((InvertedAccessor*)accessor)->SetBooleanOrOperator('|');
- ((InvertedAccessor*)accessor)->SetBooleanNotOperator('-');
- ((InvertedAccessor*)accessor)->SetBooleanLeftFence('[');
- ((InvertedAccessor*)accessor)->SetBooleanRightFence(']');
-
- resultCount = ((InvertedAccessor*)accessor)->RankedSearchBoolean((byte*)query, strlen(query), // query string
- results, MaxResultCount, // result array
- &DemoRankedProgress, 30, NULL); // progress args
- #endif
-
- time_t duration = time(NULL) - start;
- struct tm* timeStruct = localtime(&duration);
- char buffer[80];
- strftime(buffer, 80, "search time: %H hours, %M minutes and %S seconds.\n", timeStruct);
- printf(buffer);
-
- printf("%lu hits\n", resultCount); // display results
- for (uint32 i = 0; i < resultCount; i++) {
- RankedHit* hit = results[i];
- printf("%5.2f : ", hit->GetScore());
- PrintDocName(hit->GetDocument());
- printf("\n");
- delete hit;
- }
- }
-
- void DemoBooleanRankedSearch (char* query, char firstChar = NULL);
- void DemoBooleanRankedSearch (char* query, char firstChar) {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(false);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
- /*
- // make a second storage
- IAStorage* storage2 = DEMOSTORAGE("\p2.index");
- IADeleteOnUnwind delStorage2(storage2);
- storage2->Open(false);
- // make a second index in this storage
- DemoIndex index2(storage2);
- index2.Open();
- */
- const uint32 nIndices = 1;
- DEMOACCESSORINDEXCLASS* indices[nIndices]; // make indices
- indices[0] = &index;
- //indices[1] = &index2;
-
- DemoAccessor accessor(indices, nIndices); // make appropriate accessor
- accessor.firstChar = firstChar;
- accessor.Initialize();
-
- DemoBooleanRankedSearchInternal(&accessor, query);
- }
-
- void DemoFeedback (char firstChar = NULL);
- void DemoFeedback (char firstChar) {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(false);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- const uint32 nIndices = 1;
- DEMOACCESSORINDEXCLASS* indices[nIndices]; // make indices
- indices[0] = &index;
-
- DemoAccessor accessor(indices, nIndices); // make appropriate accessor
- accessor.firstChar = firstChar;
- accessor.Initialize();
-
- DocID maxID = index.GetMaxDocID();
- if (maxID > 1) {
- IADoc* doc = index.GetIDDoc(maxID / 2);
- IADeleteOnUnwind delDoc(doc);
- DemoRankedSearchInternal(&accessor, "", &doc, 1);
- }
- }
-
-
- void DemoFeedbacks(char firstChar = NULL);
- void DemoFeedbacks(char firstChar) {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(false);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- const uint32 nIndices = 1;
- const uint32 ndocs = 3;
- const uint32 loopControl = 2;
- HFSDoc* fbdocs[kMaxDocuments];
- DEMOACCESSORINDEXCLASS* indices[nIndices]; // make indices
- indices[0] = &index;
-
- DemoAccessor accessor(indices, nIndices); // make appropriate accessor
- accessor.firstChar = firstChar;
- accessor.Initialize();
-
- IAOrderedStorableIterator* docs = index.GetDocInfoIterator();
- IADeleteOnUnwind delDocs(docs);
-
- uint32 j = 0;
- for (DocInfo* di = (DocInfo*)docs->Next(); di; di = (DocInfo*)docs->Next()) {
- IADeleteOnUnwind delDi(di);
- for (int i = 0; i < ndocs; i++) {
- fbdocs[i] = (HFSDoc*) di->GetDocument();
- di = (DocInfo*)docs->Next();
- }
- DemoRankedSearchInternal(&accessor, "", (IADoc**)fbdocs, ndocs);
- if (++j > loopControl) break;
- }
- }
-
- void DemoGetDocTopic();
- void DemoGetDocTopic() {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(false);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- const uint32 nIndices = 1;
- DEMOACCESSORINDEXCLASS* indices[nIndices]; // make indices
- indices[0] = &index;
-
- DemoAccessor accessor(indices, nIndices); // make appropriate accessor
- accessor.Initialize();
-
- IATerm* results[MaxResultCount]; // allocate array for results
-
- DocID maxID = index.GetMaxDocID();
- if (maxID > 1) {
- IADoc* doc = index.GetIDDoc(maxID / 2);
- IADeleteOnUnwind delDoc(doc);
- printf("Doc: ");
- PrintDocName(doc);
- printf("\n");
-
- time_t start = ::time(NULL);
-
- RankedQueryDoc rqd(doc, &index);
- uint32 resultCount = accessor.GetDocTopic( &rqd, results, MaxResultCount,
- &DemoRankedProgress, 30, NULL);
- time_t duration = ::time(NULL) - start;
- struct tm* timeStruct = localtime(&duration);
- char buffer[80];
- strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", timeStruct);
- printf(buffer);
-
- printf("terms:");
- for (uint32 i = 0; i < resultCount; i++) {
- IATerm* term = results[i];
- printf(" %s", term->GetData());
- delete term;
- }
- printf("\n");
- }
- }
-
- void DemoStoreInits (bool force = false);
- void DemoStoreInits (bool force) {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(true); // note: opened writably
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- const uint32 nIndices = 1;
- DEMOACCESSORINDEXCLASS* indices[nIndices]; // make indices
- indices[0] = &index;
-
- DemoAccessor accessor(indices, nIndices);
-
- if (force || !accessor.IsInitializationValid()) {
- printf("Computing accessor initializations.\n");
- time_t initStartTime = time(NULL);
-
- accessor.StoreInitialization(); // store initialization
- storage->Commit(); // save changes
-
- time_t initTime = time(NULL) - initStartTime;
- struct tm* timeStruct = localtime(&initTime);
- char buffer[80];
- strftime(buffer, 80, "store inits: %H hours, %M minutes and %S seconds.\n", timeStruct);
- printf(buffer);
- } else {
- printf("Initialization is already up to date.\n");
- }
-
- }
-
- void DemoDelete ();
- void DemoDelete () {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(true);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- DocID maxID = index.GetMaxDocID();
- if (maxID > 1) {
- IADoc* doc = index.GetIDDoc(maxID / 2);
- IADeleteOnUnwind delDoc(doc);
- index.DeleteDoc(doc);
- index.Flush();
- storage->Commit(); // commit the changes
- }
- }
-
- void DemoDeleteAll ();
- void DemoDeleteAll () {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(true);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- for (DocID id = 1; id < index.GetMaxDocID(); id++) {
- IADoc* doc = index.GetIDDoc(id);
- IADeleteOnUnwind delDoc(doc);
- index.DeleteDoc(doc);
- }
- index.Flush();
- storage->Commit(); // commit the changes
- }
-
-
- void DemoCompact ();
- void DemoCompact () {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(true);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- printf("compacting...\n");
- index.Compact();
- }
-
-
- void DemoRelatedTerms(char* query);
- void DemoRelatedTerms(char* query) {
- printf (">>>>>>> The Related Terms %s\n", query);
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(false);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- const uint32 nIndices = 1;
- DEMOACCESSORINDEXCLASS* indices[nIndices]; // make indices
- indices[0] = &index;
-
- DemoAccessor accessor(indices, nIndices); // make appropriate accessor
- accessor.firstChar = '\0';
- accessor.Initialize();
-
- IATerm* termResults[MaxResultCount]; // allocate array for results
-
- uint32 resultCount = accessor.GetTermsRelated((byte*)query, strlen(query), // query string
- termResults, MaxResultCount, // result array
- &DemoRankedProgress, 30, NULL); // progress args
-
- printf("related terms:");
- for (uint32 i = 0; i < resultCount; i++) {
- IATerm* term = termResults[i];
- printf(" %s", term->GetData());
- delete term;
- }
- printf("\n");
- printf ("<<<<<<< \n");
-
- }
-
- void DemoOpen();
- void DemoOpen () {
- // make a storage
- StringPtr name = "\ptest.index";
- short vRefNum = 0;
- long dirID = 0;
-
- IAStorage * aStorage = MakeHFSStorage(vRefNum, dirID, name);
- IADeleteOnUnwind delInxStorage(aStorage);
- aStorage->Open(true);
-
- HFSCorpus* anHFSCorpus = new HFSCorpus(HFSCorpusType);
- InVecIndex anInVecIndex(aStorage, anHFSCorpus, new SimpleAnalysis());
-
- anInVecIndex.Open();
- }
-
- /// the main procedure
-
- void main() {
-
- #if __profile__
- #ifdef powerc
- ProfilerInit(collectDetailed, PPCTimeBase, 1500, 50);
- #else
- ProfilerInit(collectSummary, microsecondsTimeBase, 1500, 50);
- #endif
- #endif
-
- IATry {
- // DemoOpen();
-
- DemoStoreInits(); // for profile
-
-
- DemoRankedSearch("water product", NULL);
- // DemoRankedSearch("Furnace creek", NULL);
- #ifdef DEMOINDEXISINVERTED
- DemoBooleanRankedSearch("water | product", NULL);
- DemoBooleanRankedSearch("[fluff+product]|[water+ocean ]", NULL);
-
- DemoBooleanRankedSearch("creek | apple", NULL);
- DemoBooleanRankedSearch("[furnace+creek]|[apple+bike] ", NULL);
-
- DemoBooleanRankedSearch("water | product", NULL);
- DemoBooleanRankedSearch("[fluff+product]|[water+ocean ]", NULL);
-
- DemoBooleanRankedSearch("[creek | apple]", NULL);
- DemoBooleanRankedSearch("[furnace+creek]|[apple+bike] ", NULL);
-
- #endif
- DemoRelatedTerms ("microsoft");
- DemoGetDocTopic();
- DemoFeedback();
-
- // DemoDeleteAll();
- // DemoDelete();
- // DemoCompact();
-
- }
- IACatch (const IAException& exception) {
- printf("Caught exception: \n", exception.What());
- }
-
- #if __profile__
- ProfilerDump("\pDemoAccessor.prof");
- ProfilerTerm();
- #endif
-
- #ifdef IADEBUG
- IAReportMemoryUsage();
- #endif
-
- #ifdef DEBUG_NEW
- DebugNewReportLeaks();
- #endif
- }
-